//
//  LoginHistoryAccountView.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/3/30.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "LoginHistoryAccountView.h"

@interface LoginHistoryAccountView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *contentView;

@property(nonatomic ,strong)NSMutableArray *dataArray;

@end

@implementation LoginHistoryAccountView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    self.backgroundColor = lClearColor;
    self.contentView = [[UIView alloc] init];
    self.contentView.layer.shadowColor = rgba(0, 0, 0, 0.10).CGColor;
    self.contentView.layer.shadowOpacity = 1;
    self.contentView.layer.shadowRadius = 15.0;
    self.contentView.layer.shadowOffset = CGSizeMake(0.11, 0.51);
    self.contentView.layer.cornerRadius = 15;
    self.contentView.frame = CGRectMake(30,88 + 201 + 55,lScreenWidth - 60,self.dataArray.count*52.00);
    [self.contentView addSubview:self.tableView];
    [self addSubview:self.contentView];
    return self;
    
}
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth - 60, self.dataArray.count*52.00) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:lWhiteColor];
        [_tableView setSeparatorColor:lClearColor];
        [_tableView setAllCornersWithCornerRadius:15];
    
    }
    
    return _tableView;
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];
    if(indexPath.row + 1 < self.dataArray.count){
        [cell addSubview:separateLineWithColor(CGRectMake(15, 52-0.5, lScreenWidth - 75, 0.5), rgba(229, 229, 229, 1))];
    }
   
    UIButton *btn = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(lScreenWidth - 60 - 46, 0, 46, 45);
        [button setImage:[UIImage imageNamed:@"ic_delete"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    btn.tag = indexPath.row + 2000;
 
    [cell.contentView addSubview:btn];
    NSDictionary *dic = self.dataArray[indexPath.row];
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(15, 0, lScreenWidth - 75 - 46, 45);
    label.numberOfLines = 0;
    [cell.contentView addSubview:label];
    label.font = RFontWithSize(16);
    label.textColor = rgba(51, 51, 51, 1);
    label.text = dic[@"account"];
    label.textAlignment = NSTextAlignmentLeft;
    
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 52;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.block)self.block(self.dataArray[indexPath.row]);
    [self removeFromSuperview];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}
- (void)deleteClick:(UIButton *)btn
{
    NSString *name = self.dataArray[btn.tag - 2000];
    [self.dataArray removeObject:name];
    [LoginHistoryAccountView deleteHistoryAccountWithAcount:@[name]];
    [self.tableView reloadData];
    self.contentView.height = self.dataArray.count*52.00;
    self.tableView.height = self.dataArray.count*52.00;
}
- (void)show
{
    self.dataArray = nil;
    self.contentView.height = self.dataArray.count*52.00;
    self.tableView.height = self.dataArray.count*52.00;
    
}
+ (void)deleteHistoryAccountWithAcount:(NSArray *)accounts
{
  
    NSMutableArray *newArray = [LoginHistoryAccountView getHistoryAccounts];
    [newArray removeObjectsInArray:accounts];
    [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:USER_ARRAY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
+ (NSMutableArray *)getHistoryAccounts
{
    NSArray *accountArray = [[NSUserDefaults standardUserDefaults] objectForKey:USER_ARRAY];
    NSMutableArray *newArray;
    if (accountArray.count != 0)
    {
        newArray = [NSMutableArray arrayWithArray:accountArray];
    }
    else
    {
        newArray = [NSMutableArray array];
    }
    return newArray;
}
+ (void)saveHistoryAccountWithAcount:(NSArray *)accounts
{
    NSMutableArray *newArray = [LoginHistoryAccountView getHistoryAccounts];
    if([newArray containsObject:accounts.firstObject])
    {
        return;
    }
    [newArray addObjectsFromArray:accounts];
    [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:USER_ARRAY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray)
    {
        NSArray *accountArray = [[NSUserDefaults standardUserDefaults] objectForKey:USER_ARRAY];
        if (accountArray.count != 0)
        {
            _dataArray = [NSMutableArray arrayWithArray:accountArray];
        }
        else
        {
            _dataArray = [NSMutableArray array];
        }
    }
    
    return _dataArray;
}
@end
